#include <stdio.h>
#include <unistd.h>
#include "sys_util.h"
#include "deamon_util.h"

int read_config(const char* pFileName)
{
	FILE* fp=fopen(pFileName, "r");
	if(fp == NULL)
	{
		printf("Can't open config file\n");
		return -1;
	}

	int i = 0;
	int j = 0;
	int len;
	char *linebuf = NULL;
	while( get_line(&linebuf, &len, fp) != -1)
	{
		if(linebuf[0] == '#')
			continue;
		if(strlen(linebuf) <= 2)
			continue;
		
		char buf[1024] = "";
	
//	this get redis param
		if(strstr(linebuf, "REDIS_POOL_SIZE")){
			get_word(linebuf, "REDIS_POOL_SIZE", buf);
			server_cfg.REDIS_POOL_SIZE = atoi(buf);
		}
		else if(strstr(linebuf, "REDIS_SERVER_NUM")){
			get_word(linebuf, "REDIS_SERVER_NUM", buf);
			server_cfg.REDIS_SERVER_NUM = atoi(buf);
		}
		else if(strstr(linebuf, "REDIS_SERVER_HOST")){
			get_word(linebuf, "REDIS_SERVER_HOST", server_cfg.redis_cfg[i++].REDIS_SERVER_HOST);
		}
		else if(strstr(linebuf, "REDIS_SERVER_PORT")){
			get_word(linebuf, "REDIS_SERVER_PORT", buf);
			server_cfg.redis_cfg[j++].REDIS_SERVER_PORT = atoi(buf);
		}
	
// 	this get aprpool param			
		else if(strstr(linebuf, "APR_SESSION_MAXSIZE")){
			get_word(linebuf, "APR_SESSION_MAXSIZE", buf);
			server_cfg.APR_SESSION_MAXSIZE = atol(buf);
		}
		else if(strstr(linebuf, "APR_REQUIRE_MAXSIZE")){
			get_word(linebuf, "APR_REQUIRE_MAXSIZE", buf);
			server_cfg.APR_REQUIRE_MAXSIZE = atol(buf);
		}
		
//	this get webserver param
		else if(strstr(linebuf, "WEB_SERVER_HOST")){
			get_word(linebuf, "WEB_SERVER_HOST", server_cfg.WEB_SERVER_HOST);
		}
		else if(strstr(linebuf, "WEB_SERVER_PORT")){
			get_word(linebuf, "WEB_SERVER_PORT", buf);
			server_cfg.WEB_SERVER_PORT = atoi(buf);
		}
		else if(strstr(linebuf, "EPOLL_SD_SIZE")){
			get_word(linebuf, "EPOLL_SD_SIZE", buf);
			server_cfg.EPOLL_SD_SIZE = atoi(buf);
		}
		else if(strstr(linebuf, "EPOLL_FORK_NUM")){
			get_word(linebuf, "EPOLL_FORK_NUM", buf);
			server_cfg.EPOLL_FORK_NUM = atoi(buf);
		}
//	other
		else 
			;
	}

	fclose(fp);

	print_config();

	return 0;
}

void print_config()
{
	printf("/*****this is web server param***********/\n");
	printf("web server: %s\n", server_cfg.WEB_SERVER_HOST);
	printf("web server port: %d\n", server_cfg.WEB_SERVER_PORT);
	printf("web server epoll sd size: %d\n", server_cfg.EPOLL_SD_SIZE);
	printf("web server epoll fork num: %d\n", server_cfg.EPOLL_FORK_NUM);

	
	printf("/*****this is apr pool param***********/\n");
	printf("apr session memory size: %d\n", server_cfg.APR_SESSION_MAXSIZE);
	printf("apr require memory size: %d\n", server_cfg.APR_REQUIRE_MAXSIZE);


	printf("/*****this is redis param***********/\n");
	printf("redis server num : %d\n", server_cfg.REDIS_SERVER_NUM);
	printf("redis pool size : %d\n", server_cfg.REDIS_POOL_SIZE);
	int i = 0;
	while(i < server_cfg.REDIS_SERVER_NUM)
	{
		printf("/************redis %d :********/\n", i);
		printf("redis%d host: %s\n", i, server_cfg.redis_cfg[i].REDIS_SERVER_HOST);
		printf("redis%d port: %d\n", i, server_cfg.redis_cfg[i].REDIS_SERVER_PORT);
		i++;
	}
}

void show_version()
{
	printf("web server 1.0.0 \n");
}


void show_help(){;}

void InitParam(int argc, char **argv)
{
	if(argc < 2){
		printf("param error! \n");
		exit(-1);
	}
	int o;
	int daemon = 0;
	while(-1 != (o = getopt(argc, argv, "f:hvD"))) {
		switch(o) {
		case 'f':
			read_config(optarg);
			break;
		case 'D':
			daemon = 1;
			break;
		case 'v': 
			show_version();
			exit(0);
		case 'h': 
			show_help();
			exit(0);
		default:
			show_help();
			exit(-1);
		}
	}
	
	if(daemon == 1){
		if(already_running() == 0){
			Daemon_init("epoll", "/");
		}else{
			printf("program already run! \n");
			exit(-1);
		}
	}
}




